using AdminAuthority.Attributes;

using Abc.Application;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using System.Threading.Tasks;

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers
{
    [NonValidation]
    //[AuthorizePolicy]
    [AllowAnonymous]
    [AuthorityController(typeCode: "HtAdmin", controllerDescription: "功能权限")]
    public class PermissionController : HtAdminBaseController
    {
        private readonly ILogger<PermissionController> _logger;
        private readonly IPermissionService _permissionService;

        public PermissionController(ILogger<PermissionController> logger
        , IPermissionService permissionService)
        {
            _logger = logger;
            _permissionService = permissionService;
        }

        [HttpGet]
        [AuthorityPermission(name: "列表", actions: "Index,GetList")]
        public async Task<IActionResult> Index()
        {
            return View();
        }

        //[HttpGet,HttpPost]
        //public async Task<JsonResult> GetList(PermissionQuery searchquery)
        //{
        //    var pagedlist = await _permissionService.GetAll(searchquery);
        //    return Json(pagedlist);
        //}
        [AuthorityPermission(name: "详情", actions: "Info")]
        public async Task<ActionResult> Info(int? id)
        {
            PermissionOut entity = null;
            if (id.HasValue)
            {
                entity = await _permissionService.Find(id.Value);
            }
            return View(entity ?? new PermissionOut());
        }

        [HttpPost]
        // [ValidateInput(false)]
        [AuthorityPermission(name: "编辑", actions: "Edit")]
        public async Task<JsonResult> Edit(PermissionInput input)
        {
            return Json(await _permissionService.InsertOrUpdate(input));
        }

        [HttpPost]
        [AuthorityPermission(name: "删除", actions: "Delete")]
        public async Task<ActionResult> Delete(int id)
        {
            return Json(await _permissionService.Delete(id));
        }

        //[HttpPost]
        //[AuthorityPermission(name: "设置状态", actions: "SetState")]
        //public async Task<JsonResult> SetState(int id, bool state, string type)
        //{
        //    var resultdata = await _permissionService.SetState(id, state, type);
        //    return Json(resultdata);
        //}
    }
}